package database;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

import sharedspace.ExecuteTest;
import sharedspace.MyFile;
import sharedspace.Student;

import messages.MessageSendWordTest;

public class GetFileFromClient {
	private String directory;
	
	public GetFileFromClient(String directory)
	{
		this.directory = directory;
	}
	 public  String getFileFromClient(MyFile myFile, ExecuteTest executeTest, Student student) 
	 { 
		 
		 	
			try {
				
				
				if (!new File("aes//"+directory+"//").exists())
						new File("aes//"+directory+"//").mkdir();
				if (!new File("aes//"+directory+"//"+executeTest.getExecuteTestID()+"//").exists())
						new File("aes//"+directory+"//"+executeTest.getExecuteTestID()+"//").mkdir();
				
					
				File file = new File("aes//"+directory+"//"+executeTest.getExecuteTestID()+"//"+executeTest.getExecuteTestID()+"_"+student.getUserID()+".docx");
				byte mybytearray[] = myFile.getMybytearray();
				BufferedOutputStream bos;
				bos = new BufferedOutputStream(new FileOutputStream(file));
				bos.write(mybytearray, 0 , mybytearray.length); //write out file
			    bos.flush(); //close buffer
			    bos.close();
			    return "aes//"+directory+"//"+executeTest.getExecuteTestID()+"//"+myFile.getFileName();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
	 		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
			
	      
	    } 

}
